package com.webbyit.swing;

import com.sun.java.swing.Painter;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

/**
 * A <code>JDesktopPane</code> that displays a simple fill <code>Color</code> as the
 * background when the nimbus l&f is being used. This saves many process cycles over the normal
 * background painting that makes use of vector drawing.
 *
 * @author webbyit
 */
public class SimpleFillDesktopPane extends JDesktopPane {

    @Override
    public void updateUI() {
        if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
            UIDefaults map = new UIDefaults();
            Painter<JComponent> painter = new Painter<JComponent>() {

                @Override
                public void paint(Graphics2D g, JComponent c, int w, int h) {
                    // file using normal desktop color
                    g.setColor(UIManager.getDefaults().getColor("desktop"));
                    g.fillRect(0, 0, w, h);
                }
            };
            map.put("DesktopPane[Enabled].backgroundPainter", painter);
            putClientProperty("Nimbus.Overrides", map);
        }
        super.updateUI();
    }
}
